package com.glinsoft.ticktak.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Cookier {

    /**
     * 读取相应的cookie值
     * @param key
     * @param request
     * @return
     */
    public static String getCookie(String key, HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        if(cookies==null) {
            return null;
        }
        for (Cookie cookie : cookies) {
            if(cookie.getName().equalsIgnoreCase(key)) {
                return cookie.getValue();
            }
        }
        return null;
    }

    /**
     * 设置并保存cookie
     * @param key
     * @param value
     * @param seconds
     * @param response
     */
    public static void setCookie(String key, String value, int seconds, HttpServletResponse response) {
        Cookie cookie = new Cookie(key,value);
        cookie.setMaxAge(seconds);
        cookie.setPath("/");
        response.addCookie(cookie);
    }


}
